/*********************************************************************************
CREATED BY: SCOTT FIELDER 
CREATED DATE: 4/18/2011


REVISION HISTORY:
-

***********************************************************************************/






public with sharing class FB_PersonAccount {

	
	public void managePerson(Map<string, string> fbRespMap){
		
		 try{
		 	 
		 	system.debug('************fbRespMap: ' + fbRespMap);
		 	List<Account> aList = [Select Id from Account Where OF__Facebook_UID__c =: fbRespMap.get('id')];
			RecordType rt = [Select Id From RecordType WHERE sObjectType = 'Account' AND Name ='Business Account'];
			 
			if(aList.isEmpty()){
				string FirstName = fbRespMap.get('first_name');
				string LastName = fbRespMap.get('last_name');
				string fbId = fbRespMap.get('id');
				string hometown = fbRespMap.get('hometown');
				string birthday = fbRespMap.get('birthday');
				string email = EncodingUtil.urlEncode(fbRespMap.get('email'), 'UTF-8');
				
				email  = email.replace('%5Cu0040', '@').replace('%40', '@');
				
				/* REMOVED DUE TO LIMITATION WITH SALESFORCE.COM PACKAGE UPLOAD FOR PERSON ACCOUNTS!
				Account newAccount = new Account();
                newAccount.FirstName = FirstName; 
                newAccount.LastName = LastName; 
                newAccount.OF__Person_Email__c = email;
                newAccount.RecordTypeId = rt.Id;
                newAccount.OF__Facebook_UID__c = fbId;
                newAccount.BillingCity = hometown;
                system.debug('************newAccount: ' + newAccount);
                insert newAccount;
				*/
				
				Account newAccount = new Account();
                newAccount.Name = FirstName +' '+ LastName;
                newAccount.OF__Facebook_UID__c = fbId;
                newAccount.BillingCity = hometown;
                newAccount.RecordTypeId = rt.Id;
                upsert newAccount OF__Facebook_UID__c; 
                
                Contact newContact = new Contact();
                newContact.AccountId = newAccount.Id;
                newContact.FirstName = FirstName; 
                newContact.LastName = LastName; 
                newContact.Email = email;
                newContact.OF__Facebook_UID__c = fbId;
                newContact.Birthdate = date.valueof(birthday);
                upsert newContact OF__Facebook_UID__c;
				
				
		 }
		 	
		 }catch(exception e){
			
			system.debug('Exception: ' + e);
			
				
			}
			
	}
	
	
		
	/***********************************************************
	* 						TEST METHODS
	***********************************************************/
	
	static testMethod void test_managePerson(){
		
		test.startTest(); 
		
		Map<string, string> fbRespMap = new Map<string, string>();
		
		fbRespMap.put('birthday', '01/21/1980');
		fbRespMap.put('email', 'test@gmailtest.com');
		fbRespMap.put('first_name', 'Scott');
		fbRespMap.put('hometown', 'null');
		fbRespMap.put('id', '999999999999999');
		fbRespMap.put('last_name', 'Fielder');
		fbRespMap.put('link', 'http://www.facebook.com/ceo');
		fbRespMap.put('locale', 'en_US');
		fbRespMap.put('name', 'Scott Fielder');
		fbRespMap.put('timezone', 'null');
		
		FB_PersonAccount cls = new FB_PersonAccount();
		cls.managePerson(fbRespMap);
		 
		List<Account> aList = [Select Id from Account Where OF__Person_Email__c =: fbRespMap.get('email')];
		
		test.stopTest();
		system.assert(aList.size() != 4);
		
		
	}	

}